﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2
{
    public partial class Form1 : Form
    {
        private Bank bank;
        private ATM atm;
        public Form1()
        {
            InitializeComponent();
            bank = new Bank("My Bank");
            atm = new ATM();
            atm.BigMoneyFetched += Atm_BigMoneyFetched;
            //测试账户创建
            var account = new Account("123456", 50000);
            var creditAccount = new CreditAccount("654321", 9000, 10000);
            bank.AddAccount(account);
            bank.AddAccount(creditAccount);
        }
        private void Atm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            var messageBox=MessageBox.Show($"警告: 账号 {e.Account.AccountNumber} 取走了大笔金额: {e.WithdrawAmount} 元！","大额取款警告",MessageBoxButtons.YesNo);
            if (messageBox==DialogResult.No)
            {
                throw new Exception("用户取消取款");
            }
        }

        private void withdrawBtn_Click(object sender, EventArgs e)
        {
            // 示例取款功能
            var account = bank.GetAccount(accountBox.Text);
            try
            {
                var amountToWithdraw = numericUpDown1.Value;
                atm.Withdraw(account, amountToWithdraw);
                MessageBox.Show("取款成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误: " + ex.Message);
            }
            balanceLabel.Text = account.Balance.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var account = bank.GetAccount(accountBox.Text);
            try
            {
                var amountToWithdraw = numericUpDown1.Value;
                atm.Deposit(account, amountToWithdraw);
                MessageBox.Show("存款成功！");
            }catch (BadCashException ex)
            {
                MessageBox.Show(ex.Message);
            }catch (Exception ex)
            {
                MessageBox.Show("错误: " + ex.Message);
            }
            balanceLabel.Text = account.Balance.ToString();
        }
    }
}